package com.ming_za.od_updater;

import com.ming_za.od_updater.core.updater.StateBridge;
import com.ming_za.od_updater.core.updater.UpdateState;
import com.ming_za.od_updater.core.window.LoadingState;
import com.ming_za.od_updater.core.window.PageManager;
import com.ming_za.od_updater.core.window.page.CheckUpdatePage;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

@Mod(
        modid = ODUpdaterMod.MOD_ID,
        name = ODUpdaterMod.MOD_NAME,
        version = ODUpdaterMod.VERSION
)
@Mod.EventBusSubscriber(modid = ODUpdaterMod.MOD_ID)
public class ODUpdaterMod {

    public static final String MOD_ID = "od_updater";
    public static final String MOD_NAME = "ODUpdater";
    public static final String VERSION = "1.0";

    /**
     * This is the instance of your mod as created by Forge. It will never be null.
     */
    @Mod.Instance(MOD_ID)
    public static ODUpdaterMod INSTANCE;

    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event) {
        // 重新初始化依赖配置的组件
        PageManager.renderDialog(new CheckUpdatePage());

        while (true){

            if (StateBridge.getLoadingState() == LoadingState.CONTINUE){
                if (!PageManager.noDisplay){
                    PageManager.closeDialog();
                }
                break;
            }else if (StateBridge.getLoadingState() == LoadingState.SHUTDOWN){
                if (!PageManager.noDisplay){
                    PageManager.closeDialog();
                }
                System.exit(0);
            }

            if (StateBridge.getUpdateState() != UpdateState.SUCCESS && PageManager.noDisplay){
                break;
            }

            try {
                Thread.sleep(200);
            } catch (InterruptedException ignored) {
                break;
            }
        }
    }
}
